package ink.xiaobaibai.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ink.xiaobaibai.bo.MonthlyOrderHaveCodeBo;
import ink.xiaobaibai.bo.SelectMonOrderBo;
import ink.xiaobaibai.common.NewMonthlyBo;
import ink.xiaobaibai.common.PageInfo;
import ink.xiaobaibai.dao.MonthlyfeeOrderDao;
import ink.xiaobaibai.entity.Logistics;
import ink.xiaobaibai.entity.MonthlyfeeDevice;
import ink.xiaobaibai.entity.MonthlyfeeOrder;
import ink.xiaobaibai.service.IMonthlyfeeDeviceService;
import ink.xiaobaibai.service.IMonthlyfeeOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiaobaibai
 * @since 2021-05-21
 */
@Service
public class MonthlyfeeOrderServiceImpl extends ServiceImpl<MonthlyfeeOrderDao, MonthlyfeeOrder> implements IMonthlyfeeOrderService {

    private final static String NEW_MONTHLY_PRE = "newMonthlyPre:";

    @Autowired
    private IMonthlyfeeDeviceService deviceService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public PageInfo<MonthlyfeeOrder> getOrderList(SelectMonOrderBo selectMonOrderBo) {
        if (selectMonOrderBo.getDeviceCode() != null && selectMonOrderBo.getDeviceId() == null) {
            QueryWrapper<MonthlyfeeDevice> q1 = new QueryWrapper<>();
            q1.select("id");
            q1.eq("device_code", selectMonOrderBo.getDeviceCode());
            Integer id = this.deviceService.getOne(q1).getId();
            selectMonOrderBo.setDeviceId(id);
        }
        return new PageInfo<>(this.baseMapper.getOrderList(selectMonOrderBo), this.baseMapper.getCountByOrderList(selectMonOrderBo), selectMonOrderBo.getPageSize());
    }

    @Override
    public List<MonthlyOrderHaveCodeBo> getOrderBoListByAdminUserId(Integer userId, Date startTime, Date endTime) {
        return this.baseMapper.getOrderBoListByAdminUserId(userId, startTime, endTime);
    }

    @Override
    public List<MonthlyOrderHaveCodeBo> getOrderBoListByDepId(Integer depId, Date startTime, Date endTime) {
        return this.baseMapper.getOrderBoListByDepId(depId, startTime, endTime);
    }

    @Override
    public void setNewMonthlyInfo(NewMonthlyBo newMonthlyBo, Integer userId) {
        String key = NEW_MONTHLY_PRE + userId + "@" + newMonthlyBo.getAdminUserId();
        this.stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(newMonthlyBo), 15, TimeUnit.MINUTES);
    }

    @Override
    public NewMonthlyBo getNewMonthlyInfo(Integer userId, Integer adminUserId) {
        String key = NEW_MONTHLY_PRE + userId + "@" + adminUserId;
        String s = this.stringRedisTemplate.opsForValue().get(key);
        if (s != null) {
            return JSON.parseObject(s, NewMonthlyBo.class);
        }
        return null;
    }

    @Override
    public PageInfo<Logistics> getLogisticsListByNoSend(Integer pageNum, Integer pageSize) {
        Integer count = this.baseMapper.getLogisticsListByNoSendCount();
        if (count == 0) {
            return new PageInfo<>(new ArrayList<>(), count, pageSize);
        }
        List<Logistics> list = this.baseMapper.getLogisticsListByNoSend((pageNum - 1) * pageSize, pageSize);
        return new PageInfo<>(list, count, pageSize);
    }

}
